//
//  SDServerViewController.m
//  SBooks
//
//  Created by shansander on 2016/11/12.
//  Copyright © 2016年 shansander. All rights reserved.
//

#import "SDServerViewController.h"
#import "HTTPServer.h"

#import "DDLog.h"

#import "DDTTYLogger.h"

#import "MyHTTPConnection.h"

#import "ServerTableViewCell.h"

#import "SDHttpServerManager.h"
NSString * const titleInfostring = @"服务器地址： http://";


@interface SDServerViewController ()<UITableViewDelegate,UITableViewDataSource>{
    HTTPServer * _httpServer;
}
@property (nonatomic, strong) NSMutableArray * theServerRecodeList;

@property (nonatomic, weak) IBOutlet UITableView * theTableView;

@end

@implementation SDServerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.theServerRecodeList = [[NSMutableArray alloc] init];
    
    __weak typeof(self) weak_self = self;
    [[SDHttpServerManager getHttpServer] startServerFinish:^(HTTPServer *hs) {
        [weak_self addRecodingTableView:[NSString stringWithFormat:@"%@%@:%ld",titleInfostring,[HTTPServer getIPAddress],(long)http_port]];
    } error:^{
        [weak_self addRecodingTableView:@"HTPP Server Error"];

    }];

    [self configView];
    
}

- (void)configView
{
    self.view.backgroundColor = [AppTheme getBackThemeColor];
    [self.theTableView setBackgroundView:nil];
    [self.theTableView setBackgroundColor:[UIColor clearColor]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recevieFile:) name:notification_will_rectected_file object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recevieFilesuccess:) name:notification_rected_file_success object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recevieFileFail) name:notification_rected_file_failed object:nil];
}

- (void)addRecodingTableView:(NSString * )remocding
{
    __weak SDServerViewController * weak_self = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSInteger count = self.theServerRecodeList.count;
        [self.theServerRecodeList addObject:remocding];
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:count inSection:0];
        
        [weak_self.theTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

    });
}

- (void)closeServie
{
    [[SDHttpServerManager getHttpServer] stopServer];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)recetedserver{
    [self addRecodingTableView:@"一个"];
}

- (void)recevieFile:(NSNotification *)notification{
   
    [self addRecodingTableView:@"接收到一个文件请求"];
}
- (void)recevieFilesuccess:(NSNotification *)notification
{
    id object = notification.object;
    
    NSString * filePath = (NSString *)object;
    
    [self addRecodingTableView:[filePath lastPathComponent]];
    
}
- (void)recevieFileFail{
    [self addRecodingTableView:@"文件请求失败"];
}

-(IBAction)onClose:(id)sender
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    __weak SDServerViewController * weak_self = self;
    [self dismissViewControllerAnimated:YES completion:^{
        [weak_self closeServie];
    }];
}
#pragma mark - UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    NSString * content = self.theServerRecodeList[row];
    CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width - 40;
    CGRect contentRect = [content boundingRectWithSize:CGSizeMake(screenWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return contentRect.size.height + 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.theServerRecodeList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //serverinfocell
    ServerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"serverinfocell"];
    NSInteger row = indexPath.row;
    NSString * content = self.theServerRecodeList[row];
    cell.thenameLabel.text = content;
    return cell;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
